package com.itlong.cloud.controller;


import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardQRCodeContentDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardQRCodeElevatorAuthDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncMessageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.SmartCardSyncDataTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.SmartCardErrorCodeEnum;
import com.itlong.cloud.noncore.miniprogram.IMiniProgramPushMessageServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <desc>
 *     一卡通同步-用户相关控制器.
 * </desc>
 *
 * @createDate 2017-12-20
 */
@RestController
@RequestMapping("/smartCard/syncdata/user/")
public class SmartCardUserController extends SmartCardBaseController {

    private static final Logger LOG = LoggerFactory.getLogger(SmartCardUserController.class);

    @Autowired
    IMiniProgramPushMessageServiceClient iMiniProgramPushMessageServiceClient;

    @Autowired
    RedisService redisService;
    /**
     * <desc>
     *     一卡通推送用户修改信息数据到旺龙云端
     * <desc/>
     *
     * @param smartCardSyncMessageDTO 一卡通云端同步数据.
     * @return
     * @author LouisLewis
     * @createDate 2017-12-20
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PUSH_HOUSEUSER)
    @RequestMapping(value = "userBasisInfo/push", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO receiveUserBasisInfo(SmartCardSyncMessageDTO smartCardSyncMessageDTO) {
        try {
            return this.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_用户信息】一卡通推送用户基本信息数据到云端失败" , e);
        }
    }


    /**
     * <desc>
     *     一卡通推送住户设备权限修改信息数据到旺龙云端
     * <desc/>
     *
     * @param smartCardSyncMessageDTO 一卡通云端同步数据.
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PUSH_HOUSEUSER_DEVICEAUTH)
    @RequestMapping(value = "userAuthInfo/push", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO receiveUserAuth(SmartCardSyncMessageDTO smartCardSyncMessageDTO) {
        try {
            return this.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_用户信息】一卡通推送用户设备权限信息数据到云端失败" , e);
        }
    }


    /**
     * <desc>
     *     一卡通请求获取云端用户修改信息数据
     * <desc/>
     *
     * @param projectId 项目Id
     * @param lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = "一卡通请求获取云端用户修改信息数据")
    @RequestMapping(value = "userBasisInfo/pull", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO sendUserBasisInfo(String projectId, String lastSyncSerailNos) {
        try {
             return getSmartCardSyncMessage(projectId,lastSyncSerailNos,SmartCardSyncDataTypeEnum.USER_BASIS_DATA);
        } catch (Exception e) {
            throw  new DataAccessException("【与旺龙一卡通对接_用户基本资料】一卡通请求获取云端用户基本信息数据失败", e);
        }
    }

    /**
     * <desc>
     *     一卡通请求获取云端用户设备权限修改信息数据
     * <desc/>
     *
     * @param projectId 项目Id
     * @param lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = "一卡通请求获取云端用户设备权限修改信息数据")
    @RequestMapping(value = "userAuthInfo/pull", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO sendUserAuth(String projectId, String lastSyncSerailNos) {
        try {
            return getSmartCardSyncMessage(projectId,lastSyncSerailNos,SmartCardSyncDataTypeEnum.USER_AUTH_DATA);
        } catch (Exception e) {
            throw  new DataAccessException("【与旺龙一卡通对接_用户设备权限】一卡通请求获取云端用户设备权限信息失败", e);
        }
    }

    /**
     * <desc>
     *      云端接收线下平台变更二维码的通知
     * </desc>
     *
     * @param json  访客id
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/31
     */
    @RequestMapping(value = "qrCode/update", method = RequestMethod.POST, produces = "application/json")
    public MessageVO updateQRCode(@RequestBody String json){
        try{
            LOG.info("入参json" + json);
            if (StringUtils.isBlank(json)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String visitorId = JSON.parseObject(json).getString("visitorId");
            LOG.info("visitorId" + visitorId);
            if (visitorId == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iMiniProgramPushMessageServiceClient.pushMessage(visitorId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw  new DataAccessException("【线下平台通知云端信息】变更二维码信息失败", e);
        }
    }

    /**
     * <desc>
     *      线下平台向云端推送二维码内容
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/6/2
     */
    @PostMapping(value = "/qrCode/pushContent", produces = "application/json")
    public MessageVO saveQRCodeContent(@RequestBody String json){
        try{
            LOG.info("pushContent打印入参" + json);
            SmartCardQRCodeContentDTO smartCardQRCodeContentDTO = JSON.parseObject(json, SmartCardQRCodeContentDTO.class);

            //判断必要参数非空
            if (smartCardQRCodeContentDTO == null
                    || StringUtils.isBlank(smartCardQRCodeContentDTO.getUserId()) || StringUtils.isBlank(smartCardQRCodeContentDTO.getEntrances())
                    || StringUtils.isBlank(smartCardQRCodeContentDTO.getProjectId()) || StringUtils.isBlank(smartCardQRCodeContentDTO.getProjectKey())
                    || StringUtils.isBlank(smartCardQRCodeContentDTO.getStartDate()) || smartCardQRCodeContentDTO.getValidTime() == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (smartCardQRCodeContentDTO.getElevators() != null){
                try {
                    JSON.parseArray(smartCardQRCodeContentDTO.getElevators(), SmartCardQRCodeElevatorAuthDTO.class);
                }catch (Exception e){
                    return new MessageVO(SmartCardErrorCodeEnum.ELEVATOR_AUTH_ERR.getErrorCode());
                }
            }
           iMiniProgramPushMessageServiceClient.cacheVisitor(smartCardQRCodeContentDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());

        }catch (Exception e){
            throw new DataAccessException("【线下平台通知云端信息】线下平台推送云端二维码内容信息失败", e);
        }
    }



}
